using System;
using System.Xml;
using MbUnit.Framework;
using Assert=NUnit.Framework.Assert;

namespace SDG.UnitTesting.SimpleTalk.Tests
{
    [NUnit.Framework.TestFixture]
    public class BusinessLayerTests_NUnit
    {
        [NUnit.Framework.Test]
        public void ValidateOrder_ValidOrder_ReturnsTrue()
        {
            Order o = new Order();
            o.CustomerID = 1;
            o.OrderID = 1;
            o.OrderDate = DateTime.Now.Add(new TimeSpan(0, 1, 0));

            Assert.IsTrue(BusinessLayer.ValidateOrder(o));
        }

        [NUnit.Framework.Test]
        public void ValidateOrder_ValidOrder_ReturnsTrue2()
        {
            Order o = new Order();
            o.CustomerID = 2;
            o.OrderID = 2;
            o.OrderDate = DateTime.Now.Add(new TimeSpan(1, 1, 0));

            Assert.IsTrue(BusinessLayer.ValidateOrder(o));
        }

        [NUnit.Framework.Test]
        public void ValidateOrder_ValidOrder_ReturnsTrue3()
        {
            Order o = new Order();
            o.CustomerID = 1400;
            o.OrderID = 1400;
            o.OrderDate = DateTime.Now.Add(new TimeSpan(10, 1, 10));

            Assert.IsTrue(BusinessLayer.ValidateOrder(o));
        }
    }
    
    [DataFixture]
    [XmlDataProvider(@"TestData\TestData_Valid_ValidateOrder.xml", "root")]
    public class BusinessLayerTests_MbUnit_Valid
    {
        [ForEachTest("TestData.dbo.ValidateOrder")] 
        public void x(XmlNode node)
        {
            Order o = new Order();
            //Not great, but need to get around limitations of Xml and the Xml Serialiser 
            o.CustomerID = Convert.ToInt32(node.Attributes["CustomerID"] != null ? node.Attributes["CustomerID"].InnerText : "-1");
            o.OrderID = Convert.ToInt32(node.Attributes["OrderID"] != null ? node.Attributes["OrderID"].InnerText : "-1");
            o.OrderDate = Convert.ToDateTime(node.Attributes["OrderDate"] != null ? node.Attributes["OrderDate"].InnerText : "01/01/1970");

            Assert.IsTrue(BusinessLayer.ValidateOrder(o));
        }
    }

    [DataFixture]
    [XmlDataProvider(@"TestData\TestData_Invalid_ValidateOrder.xml", "root")]
    public class BusinessLayerTests_MbUnit_InvalidData
    {
        [ForEachTest("TestData.dbo.ValidateOrder")]
        public void x(XmlNode node)
        {
            Order o = new Order();
            //Not great, but need to get around limitations of Xml and the Xml Serialiser 
            o.CustomerID = Convert.ToInt32(node.Attributes["CustomerID"] != null ? node.Attributes["CustomerID"].InnerText : "-1");
            o.OrderID = Convert.ToInt32(node.Attributes["OrderID"] != null ? node.Attributes["OrderID"].InnerText : "-1");
            o.OrderDate = Convert.ToDateTime(node.Attributes["OrderDate"] != null ? node.Attributes["OrderDate"].InnerText : "01/01/1970");

            Assert.IsFalse(BusinessLayer.ValidateOrder(o));
        }
    }
}
